//
//  SearchViewController.m
//  
//
//  Created by lanou3g on 15/11/5.
//
//

#import "SearchViewController.h"
#import "SearchFindViewController.h"
#import "SearchLostViewController.h"
#import "LostDetailViewController.h"
@interface SearchViewController ()<ViewPagerDataSource, ViewPagerDelegate,UISearchBarDelegate>

@property (nonatomic, strong)UISearchBar *searchBar;
@property (nonatomic,retain)SearchLostViewController *lostVC;
@end



@implementation SearchViewController


- (instancetype)init
{
    if (self = [super init]) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return  self;
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"FIND" object:nil];
}

- (void)viewDidLoad {
    self.dataSource = self;
    self.delegate = self;
    
    self.lostVC = [[SearchLostViewController alloc]init];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionNntification:) name:@"FIND" object:nil];
    [super viewDidLoad];
    [self addSubviews];

}
// 通知说的方法
- (void)actionNntification:(NSNotification *)notifacation
{
    if ([[notifacation.object valueForKey:@"flag"]isEqualToString:@"find"]) {
        FindDetailViewController *view = [[FindDetailViewController alloc]init];
        view.objectID = [notifacation.object valueForKey:@"objectID"];
        [self.navigationController pushViewController:view animated:YES];
    } else {
        LostDetailViewController *view = [[LostDetailViewController alloc]init];
        view.objectID = [notifacation.object valueForKey:@"objectID"];
        [self.navigationController pushViewController:view animated:YES];
    }
   
}


#pragma mark ===== viewPageControllor 代理方法
- (NSUInteger)numberOfTabsForViewPager:(ViewPagerController *)viewPager {
    return 2;
}

- (UIView *)viewPager:(ViewPagerController *)viewPager viewForTabAtIndex:(NSUInteger)index {
    UILabel *label = [UILabel new];
    label.backgroundColor = [UIColor colorWithWhite:0.773 alpha:1.000];
    label.font = [UIFont systemFontOfSize:13.0];
    NSArray *array = @[@"FIND",@"LOST"];
    label.text = [NSString stringWithFormat:@"%@", array[index]];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    [label sizeToFit];
    return label;
}


- (UIViewController *)viewPager:(ViewPagerController *)viewPager contentViewControllerForTabAtIndex:(NSUInteger)index {
    
    if(index == 0){
        SearchFindViewController *findVC = [[SearchFindViewController alloc]init];
        
        return findVC;
    }else if (index == 1) {
        return self.lostVC;
    }
    return nil;
}
- (CGFloat)viewPager:(ViewPagerController *)viewPager valueForOption:(ViewPagerOption)option withDefault:(CGFloat)value {
    switch (option) {
            // 0.0从第1页开始,1.0从第2页开始,
        case ViewPagerOptionStartFromSecondTab:
            return 0.0;
            break;
        case ViewPagerOptionCenterCurrentTab:
            return 0.0;
            break;
            // 0.0在下面,1.0在上面
        case ViewPagerOptionTabLocation:
            return 1.0;
            break;
        default:
            break;
    }
    return value;
}


- (UIColor *)viewPager:(ViewPagerController *)viewPager colorForComponent:(ViewPagerComponent)component withDefault:(UIColor *)color {
    switch (component) {
           // 条的颜色
        case ViewPagerIndicator:
            return [UIColor colorWithRed:0.341 green:0.749 blue:1.000 alpha:1.000];
            break;
            // 条背景的颜色
        case ViewPagerTabsView:
            return [UIColor colorWithWhite:0.773 alpha:1.000];
            break;
        default:
            break;
    }
    
    return color;
}
- (CGFloat)viewPagerwidth
{
    return kScreenWidth/2;
}

- (void)addSubviews{
   
   
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth-60, kNavBarHeight - 20)];
    [self.searchBar setPlaceholder:@"搜索"]; // 搜索框的占位符
    [self.searchBar setSearchTextPositionAdjustment:UIOffsetMake(10, 0)];
    self.searchBar.delegate = self;
     self.searchBar.barTintColor = [UIColor colorWithRed:0.341 green:0.749 blue:1.000 alpha:1.000];
   [self.searchBar setBarStyle:UIBarStyleDefault];
    self.navigationItem.titleView = self.searchBar;
   
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Returns"] style:UIBarButtonItemStylePlain target:self action:@selector(actionBackButton:)];
    backButton.tintColor = [UIColor whiteColor];                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
    self.navigationItem.leftBarButtonItem = backButton;
    
    
}
#pragma mark -- 点击方法
- (void)actionBackButton:(UIBarButtonItem *)sender{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}


#pragma mark -- searchBar代理方法
// 开始编辑的时候调用的代理方法
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    searchBar.showsCancelButton = YES;
    for(id cc in [searchBar subviews])
    {
        for (id zz in [cc subviews]) {
            if([zz isKindOfClass:[UIButton class]])
            {
                UIButton *btn = (UIButton *)zz;
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [btn setTitle:@"搜索" forState:UIControlStateNormal];
            }
        }
    }
}

// 搜索按钮的代理方法
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    if ([searchBar.text isEqualToString:@""]) {
        return;
    } else {
        AVQuery *queryLost = [LostMessageModel query];
//        queryLost.cachePolicy = kAVCachePolicyCacheElseNetwork;
//        queryLost.maxCacheAge = 60 * 60;
        [queryLost whereKey:@"context" containsString:searchBar.text];
        [queryLost findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"SEARCHLOST" object:@{@"array":objects}];
        }];

    AVQuery *queryFind = [FindMessageModel query];
//    queryFind.cachePolicy = kAVCachePolicyCacheElseNetwork;
//    queryFind.maxCacheAge = 60 * 60;
    [queryFind whereKey:@"context" containsString:searchBar.text];
    [queryFind findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"SEARCHFIND" object:@{@"array":objects}];
        
    }];
    }
    [self.searchBar resignFirstResponder];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
